import { Entity, Column, Index } from 'typeorm';
import { BaseEntity } from '../../../common/entity/base.entity';

export enum UserActionType {
  LOGIN = 'login',
  LOGOUT = 'logout',
  CREATE = 'create',
  UPDATE = 'update',
  DELETE = 'delete',
  BATCH_DELETE = 'batch_delete',
  QUERY = 'query',
  OTHER = 'other'
}

@Entity('user_logs')
@Index(['userId'])
@Index(['actionType'])
@Index(['createdAt'])
export class UserLog extends BaseEntity {
  @Column({
    name: 'user_id',
    type: 'varchar',
    length: 36,
    comment: '用户ID',
  })
  userId: string;

  @Column({
    name: 'username',
    type: 'varchar',
    length: 50,
    comment: '用户名',
  })
  username: string;

  @Column({
    name: 'action_type',
    type: 'varchar',
    length: 20,
    comment: '操作类型',
  })
  actionType: UserActionType;

  @Column({
    name: 'action_description',
    type: 'varchar',
    length: 255,
    comment: '操作描述',
  })
  actionDescription: string;

  @Column({
    name: 'resource_type',
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '资源类型',
  })
  resourceType?: string;

  @Column({
    name: 'resource_id',
    type: 'varchar',
    length: 36,
    nullable: true,
    comment: '资源ID',
  })
  resourceId?: string;

  @Column({
    name: 'ip_address',
    type: 'varchar',
    length: 45,
    nullable: true,
    comment: 'IP地址',
  })
  ipAddress?: string;

  @Column({
    name: 'user_agent',
    type: 'text',
    nullable: true,
    comment: '用户代理',
  })
  userAgent?: string;

  @Column({
    name: 'request_url',
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '请求URL',
  })
  requestUrl?: string;

  @Column({
    name: 'request_method',
    type: 'varchar',
    length: 10,
    nullable: true,
    comment: '请求方法',
  })
  requestMethod?: string;

  @Column({
    name: 'api_name',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '接口名称',
  })
  apiName?: string;

  @Column({
    name: 'request_params',
    type: 'json',
    nullable: true,
    comment: '请求参数',
  })
  requestParams?: any;

  @Column({
    name: 'response_data',
    type: 'json',
    nullable: true,
    comment: '响应数据',
  })
  responseData?: any;

  @Column({
    name: 'execution_time',
    type: 'int',
    nullable: true,
    default: 0,
    comment: '执行时间(ms)',
  })
  executionTime?: number;
} 